package com.neouton.crm.managerReceiptRecord.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.config.ResponseAPI;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.managerReceiptRecord.entity.ManagerReceiptRecord;
import com.neouton.crm.managerReceiptRecord.service.ManagerReceiptService;
import com.neouton.crm.pm.service.ProjectMemberService;
import com.neouton.crm.receiptRecords.entity.ReceiptRecords;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/managerReceiptRecord")
public class ManagerReceiptController {

    @Resource
    private ManagerReceiptService managerReceiptService;
    @Resource
    private ProjectMemberService projectMemberService;



    @RequestMapping("/saveManagerReceiptRecord")
    @ResponseBody
    public ResponseAPI saveManagerReceiptRecord(@RequestBody ManagerReceiptRecord managerReceiptRecord, @RequestHeader("token") String token) {
        return ResponseAPI.successData(managerReceiptService.saveManagerReceiptRecord(managerReceiptRecord,token));

    }

    @RequestMapping("/queryReceiptRecordsApprovalListInfo/{pageNum}/{pageSize}")
    @ResponseBody
    public ResponseAPI queryReceiptRecordsApprovalListInfo(@PathVariable(value = "pageNum") Integer pageNum,
                                                         @PathVariable(value = "pageSize") Integer pageSize,@RequestBody ManagerReceiptRecord managerReceiptRecord, @RequestHeader("token") String token) {

        IPage<ManagerReceiptRecord> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerReceiptRecord> list = managerReceiptService.queryReceiptRecordsApprovalListInfo(page, managerReceiptRecord, token);
        return ResponseAPI.successData(list);

    }


    @RequestMapping("/queryManagerReceiptRecordListInfo/{pageNum}/{pageSize}")
    @ResponseBody
    public ResponseAPI queryManagerReceiptRecordListInfo(@PathVariable(value = "pageNum") Integer pageNum,
                                                         @PathVariable(value = "pageSize") Integer pageSize,@RequestBody ManagerReceiptRecord managerReceiptRecord, @RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"ManagerReceiptRecord", token);
        IPage<ManagerReceiptRecord> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerReceiptRecord> list = managerReceiptService.queryManagerReceiptRecordsListInfo(page, managerReceiptRecord, userList, token);
        return ResponseAPI.successData(list);

    }

    /**
     * 查询收票记录
     * @param managerReceiptRecord
     * @return
     */
    @PostMapping("/queryManagerReceiptRecord/")
    public ResponseAPI queryManagerReceiptRecord(@RequestBody ManagerReceiptRecord managerReceiptRecord) {
        List<ManagerReceiptRecord> list =  managerReceiptService.queryManagerReceiptRecord(managerReceiptRecord);
        return ResponseAPI.successData(list);
    }

    @PostMapping("/approvedManagerReceiptRecord/")
    public ResponseAPI approvedManagerReceiptRecord(@RequestBody ManagerReceiptRecord entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(managerReceiptService.approvedManagerReceiptRecord(entity,token,false));
    }

    @PostMapping("/rejectionManagerReceiptRecord/")
    public ResponseAPI rejectionManagerReceiptRecord(@RequestBody ManagerReceiptRecord entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(managerReceiptService.rejectionManagerReceiptRecord(entity,token));
    }

    @PostMapping("/queryManagerReceiptRecordHistory/{approvalId}")
    public ResponseAPI queryManagerReceiptRecordHistory(@PathVariable(value = "approvalId") Integer approvalId) {
        return ResponseAPI.successData(managerReceiptService.queryManagerReceiptRecordHistory(approvalId));
    }

    @PostMapping("/updateReceiptTableData/{approvalId}")
    public ResponseAPI updateReceiptTableData(@PathVariable(value = "approvalId") Integer approvalId) {
        managerReceiptService.updateReceiptTableData(approvalId);
        return ResponseAPI.success();
    }

    @PostMapping("/deleteReceiptRecordById/{receiptRecordsId}")
    public ResponseAPI deletePayedRequestById(@PathVariable(value = "receiptRecordsId") Integer receiptRecordsId) {
        Boolean flag= managerReceiptService.deleteReceiptRecordById(receiptRecordsId);
        return ResponseAPI.successData(flag);
    }

    /**
     * 导出Excel
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, @RequestBody ManagerReceiptRecord entity, @RequestHeader("token") String token) throws Exception{
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"managerReceiptRecord", token);
        List<ManagerReceiptRecord> list = managerReceiptService.queryReceiptRecordsListInfo(entity, userList, token);
        managerReceiptService.exportReceiptRecordsInfo(response,list,token);
    }
}
